package com.tieshan.smartwindow.other

import android.content.Context
import android.net.ConnectivityManager
import android.util.Log
import java.net.NetworkInterface

internal object IPUtil {
    private const val TAG = "IPAddressUtil"
    fun getIPAddress(context: Context): String? {
        var ipAddress: String? = null
        try {
            val connManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connManager.activeNetworkInfo
            if (networkInfo != null && networkInfo.isConnected) {
                if (networkInfo.type == ConnectivityManager.TYPE_ETHERNET) {
                    // 获取WiFi网络的IP地址
                    ipAddress = iPAddress
                } else if (networkInfo.type == ConnectivityManager.TYPE_MOBILE) {
                    // 获取移动网络的IP地址
                    ipAddress = mobileIPAddress
                }
            }
        } catch (e: Exception) {
            Log.e(TAG, "Failed to get IP address: " + e.message)
        }
        return ipAddress
    }

    private val iPAddress: String?
        private get() {
            try {
                val networkInterfaces = NetworkInterface.getNetworkInterfaces()
                while (networkInterfaces.hasMoreElements()) {
                    val networkInterface = networkInterfaces.nextElement()
                    if (networkInterface.name.equals(
                            "eth0",
                            ignoreCase = true
                        ) || networkInterface.name.equals("eth1", ignoreCase = true)
                    ) {
                        val inetAddresses = networkInterface.inetAddresses
                        while (inetAddresses.hasMoreElements()) {
                            val inetAddress = inetAddresses.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress.address.size == 4) {
                                return inetAddress.hostAddress
                            }
                        }
                    }
                }
            } catch (e: Exception) {
                Log.e(TAG, "Failed to get WiFi IP address: " + e.message)
            }
            return null
        }
    private val mobileIPAddress: String?
        private get() {
            try {
                val networkInterfaces = NetworkInterface.getNetworkInterfaces()
                while (networkInterfaces.hasMoreElements()) {
                    val networkInterface = networkInterfaces.nextElement()
                    if (networkInterface.name.equals("rmnet0", ignoreCase = true)) {
                        val inetAddresses = networkInterface.inetAddresses
                        while (inetAddresses.hasMoreElements()) {
                            val inetAddress = inetAddresses.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress.address.size == 4) {
                                return inetAddress.hostAddress
                            }
                        }
                    }
                }
            } catch (e: Exception) {
                Log.e(TAG, "Failed to get mobile IP address: " + e.message)
            }
            return null
        }
}